package com.yijiu.trb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.response.BaseResponse;
import com.yijiu.utils.IDCheck;

/**
 * 忘记密码
 * 
 * @author TangChao
 */
public class ForgetPwdActivity extends BTitleActivity
{
	private EditText userNameEditText, cardNumEditText;
	
	private String userNameStr;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_pwd_layout);
		initWidget();
	}
	
	@Override
	protected void initWidget()
	{
		titleView.setText("忘记密码");
		userNameEditText = (EditText) findViewById(R.id.et_forget_pwd_username);
		cardNumEditText = (EditText) findViewById(R.id.et_forget_pwd_cardnum);
	}
	
	private void doDataCheck()
	{
		userNameStr = userNameEditText.getText().toString();
		String cardNumStr = cardNumEditText.getText().toString();
		if (StringUtils.isNull(userNameStr))
		{
			app.showMsg("请输入用户名");
			return;
		}
		if (StringUtils.isNull(cardNumStr))
		{
			app.showMsg("请输入身份证号码");
			return;
		}
		if (!IDCheck.checkID(cardNumStr))
		{
			app.showMsg("请输入有效的身份证号码");
			return;
		}
		queryUserInfo(userNameStr, cardNumStr);
	}
	
	/**
	 * 查询用户手机号
	 * 
	 * @param appKey
	 */
	private void queryUserInfo(String userNameString, String certNoString)
	{
		String inputStr = "service=queryUserInfoService&userName=" + userNameString + "&certNo=" + certNoString;
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData);
		BaseResponse baseResponse = new BaseResponse();
		Request request = new Request(action, baseResponse, ActionType.REQUEST_USER_MOBLIE);
		netManager.excute(request, this, this);
		showProgress("正在获取用户信息");
	}
	
	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_USER_MOBLIE:
				BaseResponse itemBaseResponse = (BaseResponse) r.getResponse();
				String mobile = itemBaseResponse.field.mobile;
				if (mobile.equals(""))
				{
					return;
				}
				Intent intent = new Intent(this, ForgetPwdTwoActivity.class);
				intent.putExtra("userName", userNameStr);
				intent.putExtra("mobile", mobile);
				startActivity(intent);
				finish();
				break;
		}
	}
	
	/**
	 * 操作失败返回的函数
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REQUEST_USER_MOBLIE:
				app.showMsg(errorMsg);
				break;
		}
	}
	
	/**
	 * 重复提交返回的函数
	 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
	
	/**
	 * 按钮点击事件
	 * 
	 * @param v
	 */
	public void forgetPwdClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btn_forget_pwd_next:
				doDataCheck();
				break;
			default:
				break;
		}
	}
}
